#!/usr/bin/env python
#
# Copy this in ~/.gimp-2.x/plug-ins/
# and restart gimp

import math
import os
import sys
from gimpfu import *

def EnlargeSprite(img, layer):
  """
  Bigger Sprite
  Params:
    fontsize: size to user for numbers
  """
  pdb.gimp_image_undo_group_start(img)
  drawable = pdb.gimp_image_flatten(img)

  #FillWhite(img, drawable)
  FillTransparent(img, drawable)
  Antialias(img, drawable)
  Scale(img, drawable, 3.0)
  Cartoon(img, drawable)
  Sharpen(img, drawable)
  Cartoon2(img, drawable)
  AutoCrop(img, drawable)
  pdb.gimp_image_undo_group_end(img)

  # Do the drop shadow as a separate operation
  pdb.gimp_image_undo_group_start(img)
  DropShadow(img, drawable)
  MergeVisibleLayers(img, drawable)
  pdb.gimp_image_undo_group_end(img)

  gimp.displays_flush()

def Scale(img, drawable, multiplier):
  w, h = pdb.gimp_image_width(img), pdb.gimp_image_height(img)
  pdb.gimp_drawable_transform_scale(drawable, 0.0, 0.0, 
                                    w * multiplier, h * multiplier, 
                                    TRANSFORM_FORWARD,
                                    INTERPOLATION_CUBIC,
                                    1, # Supersample (ignored)? 
                                    3, # Level of recursion
                                    0) # Resize adjust
  pdb.gimp_image_resize_to_layers(img)

def Antialias(img, drawable):
  """Note documentation appears incorrect."""
  pdb.plug_in_antialias(img, drawable)

def FillWhite(img, drawable):
  """Fill all white, 0, 0, must be OK location."""
  pdb.gimp_context_set_foreground((255, 255, 255))
  pdb.gimp_edit_bucket_fill(drawable, FG_BUCKET_FILL, NORMAL_MODE, 
                            100, # Opacity
                            0, # threshold
                            0, # sample_merged,
                            0, # x, y
                            0) 

def FillTransparent(img, drawable):
  """0,  0, must be OK location."""
  pdb.gimp_layer_add_alpha(drawable)
  pdb.gimp_context_set_foreground((255, 255, 255))
  pdb.gimp_fuzzy_select(drawable, 
                        0, 0, # x, y
                        15.0, # threshold
                        CHANNEL_OP_REPLACE, # operation
                        1, # antialias
                        0, # feather
                        0, # feather radius
                        0) # sample merged 
  pdb.gimp_edit_clear(drawable)
  pdb.gimp_selection_clear(img)

def Sharpen(img, drawable):
  """Use the unsharp mask to bring out the colors a bit more."""
  pdb.plug_in_unsharp_mask(img, drawable, 
                           1.0, # radius
                           0.8, # Amount,
                           0) # Threshold

def Cartoon(img, drawable):
  """Run the cartoon effect, enhancing the edges with more black."""
  pdb.plug_in_cartoon(img, drawable, 
                      6.0, # mask-radius
                      0.2) # percent black (0.0 - 1.0)

def Cartoon2(img, drawable):
  """Run the cartoon effect, enhancing the edges with more black."""
  pdb.plug_in_cartoon(img, drawable, 
                      20.0, # mask-radius
                      0.4) # percent black (0.0 - 1.0)

def DropShadow(img, drawable):
  """Note this script creates a new layer with the dropshadow (nice)."""
  pdb.script_fu_drop_shadow(img, drawable,
                            8, 8, # offset x, y
                            8, # blur radius
                            (0, 0, 0), # color (black)
                            80, # opacity
                            1) # allow resizing

def AutoCrop(img, drawable):
  """Shrink image down as small as possible."""
  pdb.plug_in_autocrop(img, drawable)

def MergeVisibleLayers(img, drawable):
  """Let's have only layer."""
  pdb.gimp_image_merge_visible_layers(img, 
                                      EXPAND_AS_NECESSARY)

register(
         "python_fu_enlarge_sprite",
         "Enlarge a small bitmap (sprite) so it looks good large.",
         "Enlarge a small bitmap (sprite) so it looks good large.",
         "Scott Kirkwood",
         "Scott Kirkwood",
         "2008",
         "<Image>/Python-Fu/Enlarge-Sprite...",
         "",
         [
         ],
         [],
         EnlargeSprite,
         )

main()
